/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Codis.java
 *
 * Created on 16/09/2011, 16:02:53
 */
package vistas;

import Renderers.LabelRenderer;
import comu.Utilitats;
import controlador.CollitaControlador;
import controlador.ProduccioControlador;
import controlador.ProducteControlador;
import entity.Produccio;
import entity.Producte;
import extraccionsDades.Extraccions;
import java.lang.String;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public class Codis extends javax.swing.JFrame implements Observer {

    Menu menu;
    
    ProducteControlador productes;
    
    /** Creates new form Codis */
    public Codis(Menu menu) {
        this.menu = menu;
        this.productes = new ProducteControlador();
        initComponents();
        update(null, null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listProductes = new javax.swing.JList();
        btTreure = new javax.swing.JButton();
        btAfegir = new javax.swing.JButton();
        btBorrar = new javax.swing.JButton();
        dcData = new com.toedter.calendar.JDateChooser();
        btAvui = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btObtenirCodis = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listSelected = new javax.swing.JList();
        btMenu2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText(Textos.CODIS_LABEL_LLISTA_PRODUCTES);

        listProductes.setCellRenderer(new LabelRenderer());
        listProductes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listProductesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listProductes);

        btTreure.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vistas/textImg/flecha_treure_45x45.png"))); // NOI18N
        btTreure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTreureActionPerformed(evt);
            }
        });

        btAfegir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vistas/textImg/flecha_afegir_45x45.png"))); // NOI18N
        btAfegir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAfegirActionPerformed(evt);
            }
        });

        btBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vistas/textImg/eliminar_45x45.png"))); // NOI18N
        btBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBorrarActionPerformed(evt);
            }
        });

        dcData.setDate(GregorianCalendar.getInstance().getTime());

        btAvui.setText(Textos.CODIS_BOTO_AVUI);
        btAvui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvuiActionPerformed(evt);
            }
        });

        jLabel2.setText(Textos.CODIS_LABEL_LLISTA_SELEC);

        btObtenirCodis.setText(Textos.CODIS_BOTO_OBTENIR_CODIS);
        btObtenirCodis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btObtenirCodisActionPerformed(evt);
            }
        });

        listSelected.setCellRenderer(new LabelRenderer());
        listSelected.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listSelectedValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(listSelected);

        btMenu2.setText(Textos.COMU_BOTO_MENU);
        btMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenu2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(jScrollPane1)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(dcData, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btAvui, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(btBorrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btTreure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(btAfegir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(btObtenirCodis, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btMenu2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(88, 88, 88)
                        .add(btAfegir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(btTreure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(btBorrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 377, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 377, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btAvui, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                        .add(btObtenirCodis, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                        .add(btMenu2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(dcData, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenu2ActionPerformed
        if (menu != null) {
            menu.setVisible(true);
            this.setVisible(false);
        }
}//GEN-LAST:event_btMenu2ActionPerformed

    private void btAvuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvuiActionPerformed
        dcData.setDate(GregorianCalendar.getInstance().getTime());
    }//GEN-LAST:event_btAvuiActionPerformed

    private void btAfegirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAfegirActionPerformed
        Object[] selected = listProductes.getSelectedValues();
        for (Object object : selected) {
            DefaultListModel modelSelect = (DefaultListModel) listSelected.getModel();
            modelSelect.addElement(object);
            DefaultListModel modelProductes = (DefaultListModel) listProductes.getModel();
            modelProductes.removeElement(object);
        }
    }//GEN-LAST:event_btAfegirActionPerformed

    private void btTreureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTreureActionPerformed
        Object[] desSelected = listSelected.getSelectedValues();
        for (Object object : desSelected) {
            DefaultListModel modelProductes = (DefaultListModel) listProductes.getModel();
            modelProductes.addElement(object);
            DefaultListModel modelSelected = (DefaultListModel) listSelected.getModel();
            modelSelected.removeElement(object);
        }
    }//GEN-LAST:event_btTreureActionPerformed

    private void btBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBorrarActionPerformed
        update_productes();
    }//GEN-LAST:event_btBorrarActionPerformed

    private void listProductesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listProductesValueChanged
        listSelected.clearSelection();
    }//GEN-LAST:event_listProductesValueChanged

    private void listSelectedValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listSelectedValueChanged
        listProductes.clearSelection();
    }//GEN-LAST:event_listSelectedValueChanged

    private void btObtenirCodisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btObtenirCodisActionPerformed
        ListModel selected = listSelected.getModel();
        if(selected.getSize()>0){ //hi han productes seleccionats
            Date data = dcData.getDate();
            if(data!=null){
                ProduccioControlador produccions = new ProduccioControlador();
                String[] codis = new String[selected.getSize()];
                for(int i = 0; i<selected.getSize();i++){
                    Producte producte = (Producte) selected.getElementAt(i);
                    Produccio produccio = produccions.findProduccionEntitiByLastCollitaForProducte(producte);
                    if(produccio == null){
                        produccio = produccions.findFirstProduccionEntitiByProducte(producte);
                    }
                    codis[i] = CollitaControlador.getTrazabilitatCodeFalse(produccio,data);
                }
                //crear l'excel amb els productes i amb els codis.
                boolean ExtracCorrecte = Extraccions.ProductesYCodis(listSelected.getModel(),codis, data,this);
                if(ExtracCorrecte){
                    JOptionPane.showMessageDialog(this, Textos.CODIS_INFO_EXTRACCIO_CORRECTE, "", JOptionPane.INFORMATION_MESSAGE, new javax.swing.ImageIcon(getClass().getResource("/vistas/textImg/ok_64x64.png")));
                }
            }else{
                JOptionPane.showMessageDialog(this, Textos.CODIS_WARNING_SELECT_DATA, "", JOptionPane.WARNING_MESSAGE);
            }
        }else{ //No hi ha cao producte seleccionat
            JOptionPane.showMessageDialog(this, Textos.CODIS_WARNING_SELECT_PRODUCT, "", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btObtenirCodisActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAfegir;
    private javax.swing.JButton btAvui;
    private javax.swing.JButton btBorrar;
    private javax.swing.JButton btMenu2;
    private javax.swing.JButton btObtenirCodis;
    private javax.swing.JButton btTreure;
    private com.toedter.calendar.JDateChooser dcData;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listProductes;
    private javax.swing.JList listSelected;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object o1) {
        update_productes();
    }
    
    private void update_productes(){
        listProductes.setModel(Utilitats.getListModel(productes.findProducteEntitiesAZWithProduccio()));
        listSelected.setModel(new DefaultListModel());
        dcData.setDate(GregorianCalendar.getInstance().getTime());
    }
}
